<template>
  <div class="content">
    <div class="common-seach-wrap" :style="{ marginBottom: '10px' }">
      <el-button
        type="primary"
        icon="Plus"
        @click="handleAdd(0)"
        v-auth="'study/video/add'"
        >添加视频</el-button
      >
    </div>
    <el-table :data="tableData" row-key="id" height="100%" stripe>
      <el-table-column type="index" label="#" fixed="left" />
      <el-table-column prop="title" label="标题" fixed="left" />
      <el-table-column label="视频">
        <template #default="scope">
          <video
            :src="scope.row.video"
            :style="{ width: '200px', height: '200px' }"
            controls
          ></video>
        </template>
      </el-table-column>
      <el-table-column prop="create_time" sortable label="创建时间" />
      <el-table-column prop="update_time" sortable label="最后修改" />
      <!-- <el-table-column prop="sort" sortable label="排序" /> -->
      <el-table-column label="操作" fixed="right" width="200">
        <template #default="scope">
          <el-button
            type="primary"
            link
            icon="Edit"
            v-auth="'study/video/edit'"
            @click="handleAdd(scope.row.id)"
          >
            编辑
          </el-button>
          <el-button
            type="danger"
            link
            icon="Delete"
            @click="del(scope.row.id)"
            v-auth="'study/video/del'"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-divider border-style="none" />
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      background
      :current-page="curPage"
      :page-size="pageSize"
      layout="total, sizes,prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <Drawer ref="detailDraw" />
    <AddDrawer ref="addDraw" @getTable="getTableInfo" />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, defineEmits, reactive, watch } from "vue";
import AddDrawer from "./component/addDialog.vue";
import { ElMessage, ElMessageBox, TabPaneName } from "element-plus";
import { delApi, getListAPi } from "@/api/company/workVideo";
const addDraw = ref<any>(null);
const query = reactive({});
const total = ref(0);
watch(
  query,
  () => {
    getTableInfo();
  },
  { deep: true }
);
const curPage = ref(1);
const pageSize = ref(20);
const tableData = ref([]);
onMounted(() => {
  getTableInfo();
});
const handleSizeChange = (val: number) => {
  pageSize.value = val;
  getTableInfo();
};
const handleCurrentChange = (val: number) => {
  curPage.value = val;
  getTableInfo();
};
function getTableInfo() {
  getListAPi({
    page: curPage.value,
    page_size: pageSize.value,
    ...query,
  }).then((res) => {
    tableData.value = res.data.data;
    total.value = res.data.total;
  });
}

function handleAdd(id: number) {
  addDraw.value.dialogOpen(id);
}
function del(id: number) {
  ElMessageBox.confirm("确定删除该视频？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    delApi(id).then((res: any) => {
      if (res.code == 1) {
        ElMessage.success("删除成功");
        getTableInfo();
      } else {
      }
    });
  });
}
defineExpose({
  getTableInfo,
});
</script>

<style lang="scss">
.el-tabs {
  .el-tabs__new-tab {
    width: auto !important;
  }
}

.table-box {
  height: calc(100% - 200px);
}
</style>
